﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ComboBox = System.Windows.Forms.ComboBox;

namespace DXEasyAccount.Common.Common
{
    public class ConfigDictionary
    {
        /// <summary>
        /// 字典集合
        /// </summary>
        public Dictionary<string, string> Items { get; set; }
        /// <summary>
        /// 创建一个对象
        /// </summary>
        public static ConfigDictionary Create(Dictionary<string, string> dic)
        {
            ConfigDictionary cd = new ConfigDictionary();
            cd.Items = dic;
            return cd;
        }
        /// <summary>
        /// 根据key获取对应的值
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">key</param>
        /// <returns>返回值</returns>
        public string GetStringValueByKey(string key)
        {
            return Items.ContainsKey(key) ? Items[key] : string.Empty;
        }
        public int GetIntValueByKey(string key)
        {
            return Items.ContainsKey(key) ? Convert.ToInt32(Items[key]) : -1;
        }
    }


    public enum ControlStatus
    {
        /// <summary>
        /// 清空控件
        /// </summary>
        ReSet,
        /// <summary>
        /// 修改控件内容
        /// </summary>
        Edit,
        /// <summary>
        /// 只读
        /// </summary>		
        ReadOnly
    }

    public enum FormShowMode
    {
        /// <summary>
        /// 连续添加
        /// </summary>
        ContinueAdd,
        /// <summary>
        /// 只添加一次
        /// </summary>
        AddOne,
        /// <summary>
        /// 连续显示,可编辑
        /// </summary>
        ContinueDisplay,
        /// <summary>
        /// 显示当前一条实体,可编辑
        /// </summary>
        DisplayCurrent,
        /// <summary>
        /// 连续只读浏览
        /// </summary>
        ReadOnlyForAll,
        /// <summary>
        /// 当前一条实体只读显示
        /// </summary>
        ReadOnlyForOne
    }

    
    


    public class WinFormHelper
    {
        #region 添加右键菜单
        /// <summary>
        /// 添加右键菜单
        /// </summary>
        /// <param name="names">菜单名称</param>
        /// <param name="texts"></param>
        /// <param name="onclick"></param>
        /// <returns></returns>
        public static ContextMenuStrip GetContextMenuStrip(string[] names, string[] texts, EventHandler[] onclick)
        {
            ContextMenuStrip CellcontextMenuStrip = new ContextMenuStrip();
            for (int i = 0; i < names.Length; i++)
            {
                ToolStripMenuItem toolStripMenu = new ToolStripMenuItem();
                toolStripMenu.Name = names[i];
                toolStripMenu.Size = new System.Drawing.Size(180, 70);
                toolStripMenu.Text = texts[i];
                toolStripMenu.Click += new EventHandler(onclick[i]);
                CellcontextMenuStrip.Items.Add(toolStripMenu);
            }
            return CellcontextMenuStrip;
        }
        #endregion

        #region 设置控件只能输入数字
        /// <summary>
        /// 设置控件只能输入数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SetControlOnlyValue(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)8 || e.KeyChar == '.' || e.KeyChar == '-');
            if (!e.Handled)
                (sender as TextBox).Tag = (sender as TextBox).Text;//记录最后一次正确输入
        }
        /// <summary>
        /// 只能输入正数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SetControlOnlyZS(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)8 || e.KeyChar == '.');
            if (!e.Handled)
                (sender as TextBox).Tag = (sender as TextBox).Text;//记录最后一次正确输入
        }
        #endregion

        #region 重置控件
        public static void ResetFormControls(object sender, EventArgs e, Control cur)
        {
            foreach (var item in cur.Controls)
            {
                if (item.GetType() == typeof(TextBox))
                {
                    ((TextBox)item).Text = string.Empty;
                }
                else if (item.GetType() == typeof(ComboBox))
                {
                    ((ComboBox)item).SelectedIndex = -1;
                }
                else if (item.GetType() == typeof(GroupBox))
                {
                    foreach (var items in ((GroupBox)item).Controls)
                    {
                        if (items.GetType() == typeof(TextBox))
                        {
                            ((TextBox)items).Text = string.Empty;
                        }
                        else if (item.GetType() == typeof(ComboBox))
                        {
                            ((ComboBox)items).SelectedIndex = -1;
                        }
                        else
                        {
                        }
                    }
                }
                else
                {

                }
            }
        }

        public static void ResetXtraFormControls(Control cur)
        {
            foreach (Control c in cur.Controls)
            {
                var textEdit = c as TextEdit;
                var dateEdit = c as DateEdit;
                var memoEdit = c as MemoEdit;
                var calcEdit = c as CalcEdit;
                var lookUpEdit = c as LookUpEdit;
                var searchLookUpEdit = c as SearchLookUpEdit;

                if (textEdit != null)
                    ((TextEdit)c).EditValue = null;
                if (dateEdit != null)
                    ((DateEdit)c).EditValue = null;
                if (memoEdit != null)
                    ((MemoEdit)c).EditValue = null;
                if (calcEdit != null)
                    ((CalcEdit)c).EditValue = 0;
                if (lookUpEdit != null)
                    ((LookUpEdit)c).EditValue = null;
                if (searchLookUpEdit != null)
                    ((SearchLookUpEdit)c).EditValue = null;

                if (c.HasChildren)
                    ResetXtraFormControls(c);
            }
        }
        #endregion

       

        #region DataGridView单元格动态求值
        /// <summary>
        /// 鼠标选中单元格动态求值
        /// </summary>
        public static string[] GetDynamicSecletedInfo(DataGridView dgv)
        {
            double SelectedCellTotal = 0;//所选单元格中数据的和值
            ArrayList selectRowsCount = new ArrayList();//行统计//dgv.SelectedColumns.Count 
            ArrayList selectColumsCount = new ArrayList();//列统计
            try
            {
                for (int counter = 0; counter < dgv.SelectedCells.Count; counter++)
                {
                    if (dgv.SelectedCells[counter].Value != null)
                    {
                        string rowNum = dgv.SelectedCells[counter].RowIndex.ToString();
                        string colNum = dgv.SelectedCells[counter].ColumnIndex.ToString();
                        if (!selectRowsCount.Contains(rowNum))//不包括
                        {
                            selectRowsCount.Add(rowNum);//添加
                        }
                        if (!selectColumsCount.Contains(colNum))
                        {
                            selectColumsCount.Add(colNum);
                        }
                        Type selectCellType = dgv.SelectedCells[counter].ValueType;
                        if (selectCellType == typeof(double) || (selectCellType == typeof(decimal)) ||
                            (selectCellType == typeof(int)) || (selectCellType == typeof(float)) || (selectCellType == typeof(byte)))
                        {
                            if (dgv.SelectedCells[counter].Value.ToString().Trim() != "")
                            {
                                //选定的数据不能为空，或者不能为null，否则会出现异常
                                SelectedCellTotal += double.Parse(dgv.SelectedCells[counter].Value.ToString());
                            }
                        }
                    }
                }
                return new string[] {SelectedCellTotal.ToString("F2"),selectRowsCount.Count.ToString(), selectColumsCount.Count.ToString (),
                dgv.SelectedCells.Count.ToString ()};
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
            }
        }
        #endregion

        #region DataGridView添加列样式
        /// <summary>
        /// 返回一个ComboBox列
        /// </summary>
        /// <param name="dataSource">ComboBox填充的列表</param>
        /// <param name="dataPropertyName">绑定数据的属性</param>
        /// <param name="DispalyName">列的显示名称</param>
        /// <param name="width">列宽度</param>
        public static DataGridViewComboBoxColumn CreateDgvComboBox(string[] dataSource, string dataPropertyName,
            string DispalyName, Type valueType, int width = 80)
        {
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.DataSource = dataSource;
            combo.DataPropertyName = dataPropertyName;
            combo.Name = DispalyName;
            combo.ValueType = valueType;
            combo.Width = width;
            return combo;
        }
        public static DataGridViewTextBoxColumn CreateDgvTextBox(string dataPropertyName, string DispalyName, Type valueType, int width = 80)
        {
            DataGridViewTextBoxColumn combo = new DataGridViewTextBoxColumn();
            combo.DataPropertyName = dataPropertyName;
            combo.Name = DispalyName;
            combo.ValueType = valueType;
            combo.Width = width;
            return combo;
        }
        public static DataGridViewCheckBoxColumn CreateDgvCheckBox(string dataPropertyName, string DispalyName, int width = 80)
        {
            DataGridViewCheckBoxColumn combo = new DataGridViewCheckBoxColumn();
            combo.DataPropertyName = dataPropertyName;
            combo.Name = DispalyName;
            combo.ValueType = typeof(bool);
            combo.Width = width;
            return combo;
        }
        #endregion
    }


}
